import { newNode } from './data';
/**
 *  自定义左侧工具栏
 */
export default class CustomPalette {
  constructor(bpmnFactory, create, elementFactory, palette, translate) {
    this.bpmnFactory = bpmnFactory;
    this.create = create;
    this.elementFactory = elementFactory;
    this.translate = translate;
    // 指定这是一个palette
    palette.registerProvider(this);
  }
  // 返回绘制的
  getPaletteEntries(element) {
    let actions = {};
    const { bpmnFactory, create, elementFactory, translate } = this;
    // 创建节点函数
    function createActions(type) {
      return function (event) {
        const businessObject = bpmnFactory.create(type);
        const shape = elementFactory.createShape({
          type,
          businessObject,
        });
        create.start(event, shape);
      };
    }
    // 自定义节点
    newNode.map((item) => {
      const { id, group, className, title, type } = item;
      actions[id] = {
        group,
        className,
        title: translate(title),
        action: { dragstart: createActions(type), click: createActions() },
      };
    });
    return actions;
  }
}

/**
 * 注入变量
 */
CustomPalette.$inject = [
  'bpmnFactory',
  'create',
  'elementFactory',
  'palette',
  'translate',
];
